/**
 * Created with IntelliJ IDEA.
 *
 * @author: cxs
 * @date: 2023/6/27
 * @time: 19:56
 * @className：CustomerController
 * @description：客户控制器
 */
package com.bailan.bus.controller;

import com.bailan.bus.service.ICustomerService;
import com.bailan.bus.vo.CustomerVo;
import com.bailan.common.util.DataGridView;
import com.bailan.common.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("customer")
public class CustomerController {
    @Autowired
    ICustomerService customerService;

    /**
     * 查询所有客户信息
     * @param customerVo
     * @return
     * url=/customer/loadAllCustomer.action
     */
    @RequestMapping("loadAllCustomer")
    public DataGridView loadAllCustomer(CustomerVo customerVo){
        return customerService.queryAllCustomer(customerVo);
    }

    /**
     * 添加用户
     * @param customerVo
     * @return
     * url=/customer/addCustomer.action
     *
     */
    @RequestMapping("addCustomer")
    public ResultObj addCustomer(CustomerVo customerVo){
            try {
                customerVo.setCreatetime(new Date());
                this.customerService.addCustomer(customerVo);
                return ResultObj.ADD_SUCCESS;
            }catch (Exception e){
                return  ResultObj.ADD_ERROR;
            }
    }

    /**
     * 修改客户信息
     * @param customerVo
     * @return
     * url=/customer/updateCustomer.action
     */
    @RequestMapping("updateCustomer")
    public  ResultObj updateCustomer(CustomerVo customerVo){
        try {
            customerVo.setCreatetime(new Date());
            this.customerService.updateCustomer(customerVo);
            return  ResultObj.UPDATE_SUCCESS;
        }catch(Exception e){
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除单个客户信息
     * @param customerVo
     * @return
     * url=/customer/deleteOneCustomer.action
     */
    @RequestMapping("deleteCustomer")
    public  ResultObj deleteOneCustomer(CustomerVo customerVo){
        try {
            this.customerService.deleteByIdentity(customerVo.getIdentity());
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            return  ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     * @param customerVo
     * /customer/batchDeleteCustomer.action
     * @return
     */
    @RequestMapping("batchDeleteCustomer")
    public  ResultObj batchDeleteCustomer(CustomerVo customerVo){
        try {
                  this.customerService.deleteManyCustomer(customerVo.getIds());
            return  ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            return  ResultObj.DELETE_ERROR;
        }
    }
}
